package com.example.jf.jfapp.fragment.Shouye;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.jf.jfapp.R;
import com.example.jf.jfapp.adapter.ZixunaDapter;
import com.example.jf.jfapp.bean.ImformationBean;
import com.example.jf.jfapp.bean.response.ImformationResponse;
import com.example.jf.jfapp.utils.Constant;
import com.example.jf.jfapp.utils.OkHttpUtils;
import com.example.jf.jfapp.utils.RequestUtils;
import com.google.gson.Gson;

import java.io.IOException;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * 位置：
 * 作用：
 * 时间：2017/7/27
 */

public class zixunFragment extends Fragment {
    @BindView(R.id.rc_zixun)
    RecyclerView rcZixun;
    Unbinder unbinder;
    private ImformationResponse resultResponse;
    private ZixunaDapter mStatuAdapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_zixun, container, false);


        unbinder = ButterKnife.bind(this, view);
        initData();
        rcZixun.setLayoutManager(new LinearLayoutManager(getActivity()));

        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);



    }

    private void initData() {


        OkHttpClient okHttpClient = OkHttpUtils.getOkHttpClient();

//        HttpUrl.Builder builder = HttpUrl.parse(Constant.URL.INFORMATION).newBuilder();

        ImformationBean ifort = new ImformationBean("", 10, "0", "1");

        Request request = RequestUtils.requestPOST(ifort, Constant.URL.INFORMATION);

        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String result = response.body().string();
//                Log.e("", "onResponse: " +result);
                resultResponse = new Gson().fromJson(result, ImformationResponse.class);
//                ArrayList<ImformationResponse.ResultBean> resultBeen = new ArrayList<ImformationResponse.ResultBean>();
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
//                        mStatuAdapter.notifyDataSetChanged();
                        mStatuAdapter = new ZixunaDapter(getActivity(),resultResponse);
                        rcZixun.setAdapter(mStatuAdapter);
                    }
                });

            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
